#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QtNetwork/QUdpSocket>
#include "DBO/db.h"
#include "Tcp/aTcpServer.h"
#include "protocal.h"

namespace Ui {
    class weatherStation;
}

class   P2PServer;
class   StatusOperation;
class   P2PPeer;
class   ONSMessenger;
class EventOP;
class WebServerConfigDialog;
class ThreadClient;
class ThreadWriteLog;
class ThreadProcess;
class QListWidgetItem;
class Adapters;
class ThreadSensorData;
class DialogSyncDataBase;
class ClientDataProcess;
class DBOInThread;
class WebServer;
class AgentInThread;

class WeatherStation : public QDialog
{
    Q_OBJECT

public:
    explicit WeatherStation(QWidget *parent = 0);
    ~WeatherStation();
    void closeEvent(QCloseEvent *);

    void analyzeIncomeDatagram( QByteArray& datagram);

    //用于通知某个peer，让它给一个新加入拓扑树的peer提供视频数据
    void shareWithPeer(QHostAddress fromIP,quint16 fromPort,
                      QHostAddress toIP,quint16 toPort,qint64 nodeMacAddr);

    //发送UDP包给网关节点，说明连接已经断开了。
    void notifyGWDisconnected(QHostAddress  iP,quint16 port,qint64 nodeMacAddr);

    DatabaseOperation* dbo;
    Ui::weatherStation *ui;
    P2PServer   *myP2pServer;

    StatusOperation  *statusOperation;
    P2PPeer *p2PPeer;

    QUdpSocket *udpSocket;  //用于和应用程序通信
    QUdpSocket  *udpSocketForGW; //用于和网关通信

    quint8 curntThreadPeerID; //记录当前应当由哪个threadpeer来发送UDP
    ONSMessenger *onsMesger;

    QHostAddress clientIP; //保存最新的数据包来源
    quint16 clientPort;
    DBOInThread* dboInThread;

public slots:
    void analyzeGWDatagram(QByteArray datagram,QHostAddress paramGWIP,
     quint16  paramGWPort,bool isFromTcp);
    void slotApdTxt(QString text);
    void slotSetIPAnalyzeIncomeDatagram( QByteArray datagram,   QHostAddress parClientIP,
        quint16 parClientPort); //设置clientip和clientport，然后调用analyzeIncomeDatagram
    void slotShowErrorInfo(QString errorInfo);

private slots:

    void on_pushButtonClear_clicked();
    void processPendingDatagrams();
    void processGWDatagrams();
    void on_pushButtonExit_clicked();

    void slotProcessConvtedData(QByteArray gwData,
          QString appIP,quint16 appPort,qint64 nodeMacAddr,QByteArray baSNBase10);
    void slotCmdAckToApp(QByteArray dataGram,QString appIP,quint16 appPort);
    void slotShowErrorInfo(QByteArray errorInfo);
    void slotMsgBox(QString title,QString text);

    QByteArray slotSendONSMesg(QString paraURI_ObjName,QString paraURI_ObjType,
         QString paraURI_ObjOwner,QString paraURI_ObjLocation,
         QString paraServiceType,QString paraID,
         bool paraIsGW=true,QString paraPWD="");

   // void on_pushButtonAudio_clicked();

    void slotSendToNode(QByteArray data, qint64 paramMacAddr);
    void slotTimerEventForInternetHeart();

    void slotRegMeToONS();//将自己注册到ONS
    void slotQueryONS();//查询自己的IP等


    void slotClientDataProcessDone(QByteArray result,QByteArray IP,quint16 port);
   // void on_pushButtonHomePage_clicked();

    void slotProcessStarted(QByteArray processName);
    void slotProcessError(QByteArray processName,QByteArray strError);
    void slotProcessFinished(QByteArray processName,int exitCode,QByteArray strStatus);
    void slotProcessStateChanged(QByteArray processName,QByteArray strState);

    void on_listWidget_itemDoubleClicked(QListWidgetItem *item);

    void on_pBAdapterTelnet_clicked();

    void on_pBAdapterSerialPort_clicked();

    void on_pBSync_clicked();

    void slotInitStepOne();
    void slotInitStepTwo();
    void slotInitStepThree();
    void slotInitStepFour();
    void slotInitStepFive();
    void slotInitStepSix();

    void slotSendPacket2App(QByteArray packet,QString appIP,quint16 appPort);


signals:
    void sigConvProto(QByteArray appData,
                 QString hostIP,quint16 port,qint64 nodeMacAddr);
    void sigCmdAck(QByteArray packet);

    void sigStoreVideo(QByteArray data,qint64  nodeMacAddr,quint8 SN,QList <QString> *tableNameList,
                       QString *tableNamePrefix,QList <qint64>  *newMacList);

    //该信号和DOBInThread类里的对应槽函数连接
    void sigRegSensor(qint64 nodeMacAddr,qint16 sensorID,qint8 sensorOnWork);
    void sigProcessClientData(QByteArray appData,QByteArray hostIP,quint16 port);

    void sigWriteLog(QByteArray log);

    void sigProcessShow(QByteArray processName); //显示进程的界面。如果进程不在运行，则启动进程
    void sigProcessHide(QByteArray processName);   //隐藏进程的界面
    void sigProcessSensorData(QByteArray packet,QHostAddress srcAddress, quint16 srcPort);   //由子线程处理传感器数据包

    void sigPacketToAgent(QByteArray packet,QString appIP,quint16 appPort);
private:
    void connectApdatersSigSlots(); //连接适配器类的信号与槽

    QMap <QString, Protocal> mapHeadProtocal;
    QString myInternetIP,myInternetPort,myInternetPortVideo; //服务器在因特网上的UDP地址

    aTcpServer myTcpServer,myTcpServerSend;

    EventOP *eventOP;

    WebServerConfigDialog *webServerConfDlog;

    //DialogMultiMedia *dialogMultiMedia;

    QTimer *timerForInternetHeart; //定时器，发送心跳给一个公网IP
    QTimer *timerForUpdateONS; //定时器，更新自己在ONS的记录

    ThreadWriteLog* threadWriteLog;

    bool isInternetIPchanged; //记录地址是否改变，若改变则向ONS更新注册
    bool isUdpOK,isUdpGWOK;

    ThreadProcess* threadOfProcess;

    Adapters *adapters;
    DialogSyncDataBase *dialogSyncDB;
    ThreadSensorData *threadSensorData;
    QThread *threadClient;
    QThread *threadDBORelated;
    QThread *threadOfWebServer;

    ClientDataProcess* clientDataProcess;
    WebServer* webServer;

    AgentInThread* agentInThread;
    QThread *threadAgent;

};

#endif // DIALOG_H
